<!DOCTYPE HTML>
<html>
	<head>
	<title>查股网DDX</title>
	<link href="css/text.css" rel="stylesheet" type="text/css" />
	<meta charset="utf-8" />
	<style>
		.fixedBox{
				border: 1px solid #555555;				
				white-space: nowrap;	
			}
			.fixedBox::-webkit-scrollbar {
				width: 12px;     
				height: 12px;				
			}			
		table{
			width:100px; 
			border-collapse:collapse; 
			text-align:center;
		}
		td,th{border:1px solid #696969}
	</style>
	<script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript" src="DatePicker/WdatePicker.js"> </script>
	</head>
	<body>
		<div id="title"></div>
		<div id="content" class="fixedBox"></div>	
		<script>
			var bkcolor = $("body").css("backgroundColor");
			var dt = "";
			var tt = "";			
			var text = "";	
			var k = 0;
			window.onload = function() {
				text = load("A股代码.txt");
				var day = new Date();				
				mymonth = day.getMonth()+1;
				myday = day.getDate();
				if(mymonth >= 10){mymonth = mymonth;}else{mymonth = "0" + mymonth;}
				if(myday >= 10){myday = myday;}else{myday = "0" + myday;}
				tt = day.getFullYear() + "-" + mymonth + "-" + myday;
				dt = tt;			
				changeStyle();
				start();
			}
			
			function load(name) {
				var xhr = new XMLHttpRequest(),
				okStatus = document.location.protocol === "file:" ? 0 : 200;
				xhr.open('GET', name, false);
				xhr.overrideMimeType("text/html;charset=utf-8");//默认为utf-8
				xhr.send(null);
				return xhr.status === okStatus ? xhr.responseText : null;
			}
			
			function time_range(beginTime, endTime) {
				var strb = beginTime.split(":");
				if (strb.length != 2) {
					return false;
				}

				var stre = endTime.split(":");
				if (stre.length != 2) {
					return false;
				}

				var b = new Date();
				var e = new Date();
				var n = new Date();

				b.setHours(strb[0]);
				b.setMinutes(strb[1]);
				e.setHours(stre[0]);
				e.setMinutes(stre[1]);

				if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {										
					$("#ds").text('刷新……');
					setTimeout(function(){getSource()}, 10);
					console.log(true)　　　　　　
					return true;				
				} else {
					console.log(false)
				    return false;     
				}
			}　　　
   　　　
			$(function() {
				setInterval(function() {
					if(k == 0){
						time_range("09:00","15:15")	
					}												
				},
				60000);
			})			
			
			function changeStyle() {				
				var div = document.getElementById("title");				
				div.style.left= "0px"; 				
				div.style.width= "98%";	
				div.style.height= "40px";	
				div.style.fontSize= "12px";	
				
				var div = document.getElementById("content");				
				div.style.top= "60px"; 
				div.style.left= "10px"; 				
				div.style.width= "96%";	
				div.style.height= "90%";
				div.style.fontSize= "12px";	
			}		
			
			
			Date.prototype.Format = function (fmt) {
				var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
				};
				if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				return fmt;
			};
			
			function getLocalTime(nS) {			          
				return new Date(parseInt(nS*1000)).Format("hh:mm:ss")
			}

					
			function cDayFunc(dp){ 
				var date = dp.cal.getNewDateStr();            
				BookingRmk(date);//调用其它函数并带上参数                
			}
			
			var pn = -1;
			function BookingRmk(date){         
				tt = date;
				var date1 = new Date(tt);
				var date2 = new Date(dt);
				k = 1;
				if(date1.getTime() == date2.getTime()){
					k = 0;
				}
				if(date1.getTime() <= date2.getTime()){					
					var day = new Date(tt);	
					var week = day.getDay();
					if(week == 0 || week == 6){
						if(pn == 1){
							next();
						}else{
							prev();
						}					
					}
					$("#ds").text('请稍等……');
					setTimeout(function(){getSource()}, 10);
				}else{
					BookingRmk(dt);	
				}
			}
			
			function prev(){  //前一天
				pn = -1;
				var day = new Date(tt);	
				day.setTime(day.getTime() - 24*60*60*1000);	
				tt = day.Format("yyyy-MM-dd");
				BookingRmk(tt);
			}
			
			function next(){  //后一天 				
				pn = 1;
				var day = new Date(tt);	
				day.setTime(day.getTime() + 24*60*60*1000);	
				tt = day.Format("yyyy-MM-dd");
				BookingRmk(tt);							
			}
			
			function start() {
				var div = document.getElementById('title');										
				div.innerHTML = '<font size="4">查股网DDX</font><input style="margin-left:10px;width: 100px;" type="text" id="PriceDate" name="PriceDate" class="Wdate" onFocus="WdatePicker({ onpicking: cDayFunc})" />';						
				var back = '<a href="javascript:prev();"  style="padding-left:15px;font-size:16px;color:#fff;">←</a>';				
				var forward = '<a href="javascript:next();"  style="padding-left:10px;font-size:16px;color:#fff;">→</a>';
				var ds = "<span id='ds' style='padding-left:100px;color:#FFFFFF;'><span>";
				div.innerHTML += back + forward + ds + '<br><br>';
				div.innerHTML += "<p id='sx' style='margin:5px 0px 0px 10px;color:#FFFFFF;font-size:14px;'></p>";				
				document.getElementById("PriceDate").value = tt;
				$("#ds").text('请稍等……');
				setTimeout(function(){getSource()}, 10);
			}
						
			var order = 1;
			var type = 2;
			var page = 2;  //页数，越多越卡
			function getSource() {						
				var div = document.getElementById('content');
				div.innerHTML = "";
				var res = [];				
				for (var i = 1; i < page + 1; i++){																
					var url = "http://ddx.gubit.cn/xg/ddxlist2021.php?orderby=" + type + "&isdesc=" + order + "&page=" + i + "&lsdate=" + tt;
					var arr = [];
					$.ajax({
						type: "get",
						url: url,
						dataType: "json",
						async : false,
						cache:false,
						timeout:3000,					
						success: function(data) {
							//alert(JSON.stringify(data))						
							//document.write(JSON.stringify(data));							
							arr = data.data;
							if(i == 1 && k == 0){								
								tt = data.updatetime;
								$("#sx").text("更新时间：" + tt.split("(")[0]);
								tt = tt.split(" ")[0];
								dt = tt;														
							}							
							res = res.concat(arr);										
						}
					});							
				}					
				//document.write(JSON.stringify(res));			
				writeSource(res);					
			}
			
			var gl = "涨幅";  //高亮
			function writeSource(data) { 								
				res = [];				
					for (i = 0; i < data.length; i++){
						var aa = data[i];
						var dm = aa[0].toString();
						dm = dm.padStart(6, "0");
						var mc = "";
						if(text.indexOf(dm) > -1){				
							mc = text.split(dm + "|")[1]					
							mc = mc.split("\r\n")[0]					
						}
						res.push({"代码":dm,"名称":mc,"最新价":aa[1],"涨幅":aa[2],"换手率":aa[3],"量比":aa[4],"DDX1日":aa[5],"DDX3日":aa[8],"DDX5日":aa[9],"DDX10日":aa[10]});							
					}					
			
				var json = res;  
				
				//创建table					
				var table=document.createElement("table");										
				var thead=document.createElement("thead");
				table.appendChild(thead);					
				var tr=document.createElement("tr");
				thead.appendChild(tr);					
				
				var m = 0;
				var n = 0;
				for(var key in json[0]){  //表头											
					var th=document.createElement("th");						
					th.innerHTML=key;						
					tr.appendChild(th);
					if(gl == key.replace("%","")){
						n = m;
						th.style.color = "#FFFF80";	
					}					
					m++;
				}
				var tbody=document.createElement("tbody");					
				table.appendChild(tbody);
				
				//遍历json
				for(var i = 0; i < json.length; i++){		//行元素遍历			
					var tr=document.createElement("tr");						
					var p = 1;
					var dm = "";
					var mc = "";
					for(var key in json[i]){ 						
						var td=document.createElement("td");							
						
						var rc = json[i][key];
						if(p == 1){  //代码列
							dm = rc;
							tr.id = dm;
						}			
						td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "' onclick='changeCss(this,\"gg\")'>" + rc + "</a>";

						if(p == n + 1){	
							var value = parseFloat(rc.toString());	
							if(value >= 0){
								td.getElementsByTagName("a")[0].style.color = "#FF6633";
							}else{
								td.getElementsByTagName("a")[0].style.color = "#00FF00";
							}							
						}																						
						tr.appendChild(td);							
						p++;
					}														
					tbody.appendChild(tr);																
				}					
				var div = document.getElementById("content");									
				div.appendChild(table);
				table.id = "gg";
				//changeTable(table,"0");
				document.getElementById("PriceDate").value = tt;							
				makeSortable(table);				
				hover();
				$("#ds").text('');
			}
			
			function changeTable(table,ss) {	
				var trs = table.rows;
				ss = ss + '#';
				var aa = ss.split('#');
				for (var j = 0; j < aa.length; j++) {
					if(aa[j] != ""){
						for (var i = 0, len = trs.length; i < len; i++) {					
							var p = aa[j];
							var cell = trs[i].cells[p]; 
							cell.style.display = 'none';
						}
					}
				}					
			}

			var mn = type + 1;
			function makeSortable(table) {
				var headers=table.getElementsByTagName("th");
				for(var i=0;i<headers.length;i++){				
					(function(n){										
						headers[n].onclick=function(){
							//alert(n);						
							if(n != 1){
								gl = headers[n].textContent;														
								if(n <= 6){
									if(n == 0){
										type = 0;
									}else{
										type = n - 1;
									}							
								}else if(n > 6){
									type = n + 1;
								}
								if(order == 1 && n == mn){
									order = 0;									
								}else{
									order = 1;									
								}
								mn = n;
								$("#ds").text('请稍等……');
								setTimeout(function(){getSource()}, 10);
							}							
						}
					}(i));
				}
			}
			
			var trid = "";
			function changeCss(obj,id){
				var tlist = document.getElementById(id).getElementsByTagName("tr");	
				var t = obj.parentNode.parentNode.id;  //td父节点tr id				
				trid = t;
				for(var i = 0; i < tlist.length; i++){	
					if(tlist[i].id == t){						
						tlist[i].style.backgroundColor = "#483D8B";;
					}else{
						tlist[i].style.backgroundColor = bkcolor; 
					}					
				}								
			}
			
			function hover(){  //鼠标经过
				var rows = document.getElementsByTagName('tr');
				for(var i=0;i<rows.length;i++){
					rows[i].onmouseover=function(){						
						if(this.id != ""){  //过滤表头
							this.style.background="#800000";
						}
					}
					rows[i].onmouseout=function(){
						if(this.id != ""){  //过滤表头
							if(this.id != trid){  //过滤选中行
								this.style.background=bkcolor;
							}else{
								this.style.background="#483D8B";
							}
						}						
					}					
				}
			}			
			
		</script>		
	</body>
</html>